//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Security;
using DotNetNuke.Services.Vendors;
using DotNetNuke.UI.Utilities;
namespace DotNetNuke.Modules.Admin.Vendors
{
	partial class Vendors : Entities.Modules.PortalModuleBase, Entities.Modules.IActionable
	{
		protected System.Web.UI.WebControls.Label lblMessage;
		protected int CurrentPage = -1;
		protected int TotalPages = -1;
		string strFilter;
		private void BindData()
		{
			BindData(null, null);
		}
		private void BindData(string SearchText, string SearchField)
		{
			CreateLetterSearch();
			Services.Localization.Localization.LocalizeDataGrid(ref grdVendors, this.LocalResourceFile);
			if (SearchText == Services.Localization.Localization.GetString("All"))
			{
					strFilter = "";
			}
			else if(SearchText == Services.Localization.Localization.GetString("Unauthorized"))
			{
					strFilter = "";
			}
			else
			{
					strFilter = SearchText;
			}
			int PageSize = Convert.ToInt32(ddlRecordsPerPage.SelectedItem.Value);
			int TotalRecords = 0;
			Services.Vendors.VendorController objVendors = new Services.Vendors.VendorController();
			int Portal;
			if (PortalSettings.ActiveTab.ParentId == PortalSettings.SuperTabId) {
				Portal = Null.NullInteger;
			} else {
				Portal = PortalId;
			}
			if (String.IsNullOrEmpty(strFilter)) {
				if (SearchText == Services.Localization.Localization.GetString("Unauthorized")) {
					grdVendors.DataSource = objVendors.GetVendors(Portal, true, CurrentPage - 1, PageSize, ref TotalRecords);
				} else {
					grdVendors.DataSource = objVendors.GetVendors(Portal, false, CurrentPage - 1, PageSize, ref TotalRecords);
				}
			} else {
				if (SearchField == "email") {
					grdVendors.DataSource = objVendors.GetVendorsByEmail(strFilter, Portal, CurrentPage - 1, PageSize, ref TotalRecords);
				} else {
					grdVendors.DataSource = objVendors.GetVendorsByName(strFilter, Portal, CurrentPage - 1, PageSize, ref TotalRecords);
				}
			}
			grdVendors.DataBind();
			ctlPagingControl.TotalRecords = TotalRecords;
			ctlPagingControl.PageSize = PageSize;
			ctlPagingControl.CurrentPage = CurrentPage;
			string strQuerystring = "";
			if (ddlRecordsPerPage.SelectedIndex != 0) {
				strQuerystring = "PageRecords=" + ddlRecordsPerPage.SelectedValue;
			}
			if (!String.IsNullOrEmpty(strFilter)) {
				strQuerystring += "&filter=" + strFilter;
			}
			ctlPagingControl.QuerystringParams = strQuerystring;
			ctlPagingControl.TabID = TabId;
		}
		private void CreateLetterSearch()
		{
			string[] strAlphabet = {
				"A",
				"B",
				"C",
				"D",
				"E",
				"F",
				"G",
				"H",
				"I",
				"J",
				"K",
				"L",
				"M",
				"N",
				"O",
				"P",
				"Q",
				"R",
				"S",
				"T",
				"U",
				"V",
				"W",
				"X",
				"Y",
				"Z",
				Services.Localization.Localization.GetString("All"),
				Services.Localization.Localization.GetString("Unauthorized")
			};
			rptLetterSearch.DataSource = strAlphabet;
			rptLetterSearch.DataBind();
		}
		public string DisplayAddress(object Unit, object Street, object City, object Region, object Country, object PostalCode)
		{
			return Common.Globals.FormatAddress(Unit, Street, City, Region, Country, PostalCode);
		}
		public string DisplayEmail(string Email)
		{
			return HtmlUtils.FormatEmail(Email);
		}
		public string FormatURL(string strKeyName, string strKeyValue)
		{
			if (!String.IsNullOrEmpty(strFilter)) {
				return EditUrl(strKeyName, strKeyValue, "", "filter=" + strFilter);
			} else {
				return EditUrl(strKeyName, strKeyValue);
			}
		}
		protected string FilterURL(string Filter, string CurrentPage)
		{
			if (!String.IsNullOrEmpty(Filter)) {
				if (!String.IsNullOrEmpty(CurrentPage)) {
					return Common.Globals.NavigateURL(TabId, "", "filter=" + Filter, "currentpage=" + CurrentPage, "PageRecords=" + ddlRecordsPerPage.SelectedValue);
				} else {
					return Common.Globals.NavigateURL(TabId, "", "filter=" + Filter, "PageRecords=" + ddlRecordsPerPage.SelectedValue);
				}
			} else {
				if (!String.IsNullOrEmpty(CurrentPage)) {
					return Common.Globals.NavigateURL(TabId, "", "currentpage=" + CurrentPage, "PageRecords=" + ddlRecordsPerPage.SelectedValue);
				} else {
					return Common.Globals.NavigateURL(TabId, "", "PageRecords=" + ddlRecordsPerPage.SelectedValue);
				}
			}
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			grdVendors.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(grdVendors_ItemCommand);
			cmdDelete.Click += new EventHandler(cmdDelete_Click);
			ddlRecordsPerPage.SelectedIndexChanged += new EventHandler(ddlRecordsPerPage_SelectedIndexChanged);
			btnSearch.Click += new System.Web.UI.ImageClickEventHandler(btnSearch_Click);

			try {
				if (Request.QueryString["CurrentPage"] != null) {
					CurrentPage = Convert.ToInt32(Request.QueryString["CurrentPage"]);
				} else {
					CurrentPage = 1;
				}
				if (Request.QueryString["filter"] != null) {
					strFilter = Request.QueryString["filter"];
				} else {
					strFilter = "";
				}
				if (!Page.IsPostBack) {
					ClientAPI.AddButtonConfirm(cmdDelete, Services.Localization.Localization.GetString("DeleteItems"));
					if (Request.QueryString["PageRecords"] != null) {
						ddlRecordsPerPage.SelectedValue = Request.QueryString["PageRecords"];
					}
					BindData(strFilter, "username");
				}
				cmdDelete.Visible = this.IsEditable;
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}

		}
		private void grdVendors_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			try {
				if (e.CommandName == "filter") {
					strFilter = e.CommandArgument.ToString();
					CurrentPage = 1;
					txtSearch.Text = "";
					BindData(strFilter, "username");
				}
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdDelete_Click(object sender, System.EventArgs e)
		{
			try {
				VendorController objVendors = new VendorController();
				if (PortalSettings.ActiveTab.ParentId == PortalSettings.SuperTabId) {
					objVendors.DeleteVendors();
				} else {
					objVendors.DeleteVendors(PortalId);
				}
				BindData();
			} catch (Exception exc) {
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void ddlRecordsPerPage_SelectedIndexChanged(System.Object sender, System.EventArgs e)
		{
			CurrentPage = 1;
			BindData();
		}
		private void btnSearch_Click(System.Object sender, System.Web.UI.ImageClickEventArgs e)
		{
			CurrentPage = 1;
			BindData(txtSearch.Text, ddlSearchType.SelectedItem.Value);
		}
		public ModuleActionCollection ModuleActions {
			get {
				ModuleActionCollection Actions = new ModuleActionCollection();
				Actions.Add(GetNextActionID(), Services.Localization.Localization.GetString(ModuleActionType.AddContent, LocalResourceFile), ModuleActionType.AddContent, "", "", EditUrl(), false, SecurityAccessLevel.Edit, true, false);
				return Actions;
			}
		}
	}
}
